// https://leetcode.cn/problems/largest-plus-sign/
// Created by ade on 2022/11/9.
//
#include <vector>
#include <iostream>
#include <unordered_map>

using namespace std;

class Solution {
public:
    int orderOfLargestPlusSign(int n, vector <vector<int>> &mines) {
        if (n == 1) return 1 - mines.size();
        if (n == 2) {
            if (mines.size() == 4) return 0;
            return 1;
        }
        vector <vector<int>> left(n, vector<int>(n, 1));
        vector <vector<int>> right(n, vector<int>(n, 1));
        vector <vector<int>> up(n, vector<int>(n, 1));
        vector <vector<int>> down(n, vector<int>(n, 1));
        for (auto &m:mines) {
            left[m[0]][m[1]] = 0;
            right[m[0]][m[1]] = 0;
            up[m[0]][m[1]] = 0;
            down[m[0]][m[1]] = 0;
        }
        for (int i = 1; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (left[i][j] == 0) continue;
                if (left[i - 1][j] >= 1) left[i][j] = left[i - 1][j] + 1;
            }
        }
        for (int i = n - 2; i >= 0; i--) {
            for (int j = 0; j < n; j++) {
                if (right[i][j] == 0) continue;
                if (right[i + 1][j] >= 1) right[i][j] = right[i + 1][j] + 1;
            }
        }

        for (int i = 0; i < n; i++) {
            for (int j = 1; j < n; j++) {
                if (down[i][j] == 0) continue;
                if (down[i][j - 1] >= 1) down[i][j] = down[i][j - 1] + 1;
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = n - 2; j >= 0; j--) {
                if (up[i][j] == 0) continue;
                if (up[i][j + 1] >= 1) up[i][j] = up[i][j + 1] + 1;
            }
        }
        int maxI = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                int tmp = min(min(up[i][j], down[i][j]), min(left[i][j], right[i][j]));
                maxI = max(maxI, tmp);
            }
        }
        return maxI;
    }

    void show(vector <vector<int>> items) {
        for (auto &item:items) {
            for (auto i:item) {
                cout << i << ",";
            }
            cout << endl;
        }
    }
};

int main() {
    Solution so;
    vector <vector<int>> mines = {{3, 0},
                                  {3, 3}};
    cout << so.orderOfLargestPlusSign(5, mines) << endl;
    return 0;
}